// packJumpRope/mineJump.js
const app = getApp()
import {
  groupByList
} from '../utils/conmon'
import {
  getStaticImgUrl
} from '../utils/api.config'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modal: null,
    dataList: [],
    userData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "我的跳绳"
    })
    this.initUser()
  },
  initUser() {
    const userInfo = wx.getStorageSync('userData');
    const stuIndex = wx.getStorageSync('stuIndex');
    const data = userInfo.list[stuIndex]
    this.setData({
      userData: data
    })
  },

  goBindJump() {
    wx.navigateTo({
      url: '/packJumpRope/addMineList'
    })
  },

  goRescind(event) { //解除
    var dict = event.target.dataset.info
    this.modal.showModal({
      title: '温馨提示',
      modalType: 0,
      content: `确定解除 ${this.data.userData.name} 与跳绳${dict.name}(IMEI:${dict.imei})的绑定关系吗？`,
      topImageUrl: getStaticImgUrl('/new/alrt_collect.png'),
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          this.unbindRopeFuction(dict)
        }
      }
    })
  },
  unbindRopeFuction(dict) {

    wx.showLoading({
      title: '提交中...',
      icon: 'none',
    })

    var params = {
      imei: dict.imei,
    }
    console.log("解绑参数="+JSON.stringify(params)); 
    app.api.unbindRope(params).then(rest => {
        wx.hideLoading()
        console.log("解绑结果="+JSON.stringify(rest)); 
        if (rest && rest.success) {
          
          wx.showToast({
            title: '解除绑定成功',
            icon: 'none',
            duration: 2000,
            mask: true,
            complete: () => {
              this.getMineJumList()

            }
          })
        } else {
          wx.showToast({
            title: rest.errMsg || '网络异常，请稍后重试！！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      })
      .catch(r => {
        wx.hideLoading()
        console.error(r)
        wx.showToast({
          title: '网络异常，请稍后重试！',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      })

  },

  getMineJumList() {
    wx.showLoading({
      title: '加载中...',
      icon: 'none',
    })
    app.api.mySkipRope().then(rest => {
        wx.hideLoading()

        if (rest && rest.success) {
          var list = groupByList(rest.data, 'bindType')
          
          console.log('获取转换后数据', JSON.stringify(list))
          this.setData({
            dataList: list
          })
        } else {
          wx.showToast({
            title: rest.errMsg || '网络异常，请稍后重试！！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      })
      .catch(r => {
        console.error(r)
        wx.showToast({
          title: '网络异常，请稍后重试！',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      })

  },


  didClickDevice(e) { //选择设备
    const dataset = e.currentTarget.dataset

    var item = this.data.dataList[dataset.index].list[dataset.id]

    item.inputValue = item.name || '';
    item.placeholderText = '请输入' + item.name + '名称'
    var list = [item];
    this.modal.showModal({
      title: '跳绳重命名',
      modalType: 1,
      inputList: list,
      content: '',
      // topImageUrl: getStaticImgUrl('/new/alrt_collect.png'),
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          this.upDataDevice(res.data.showData.inputList)

        }
      }
    })
  },

  upDataDevice(list) {
    wx.showLoading({
      title: '提交中...',
      icon: 'none',
    })

    var params = {
      studentId: this.data.userData.studentId,
      seId: list[0].seId,
      name: list[0].inputValue,
      // bingStatus: list[0].bindStatus,
      // useStatus: list[0].useStatus,
    }
    console.log('获取canshu ', JSON.stringify(params))
    app.api.updateEquipment(params).then(rest => {
        wx.hideLoading()

        if (rest && rest.success) {
          this.getMineJumList()
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        } else {
          wx.showToast({
            title: rest.errMsg || '网络异常，请稍后重试！！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      })
      .catch(r => {
        wx.hideLoading()
        console.error(r)
        wx.showToast({
          title: '网络异常，请稍后重试！',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.modal = this.selectComponent("#modal")

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMineJumList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})